// pages/another-pay/another-pay.js
import { Job } from '../../models/job'
import { Supply } from '../../models/supply'
import { Payment } from '../../models/payment'

const jobModel = new Job()
const supplyModel = new Supply()
const paymentModel = new Payment()

Page({
  /**
   * Page initial data
   */
  data: {
    options: {},
    detail: {}
  },
  onLoad: function (options) {
    this.setData({
      options
    })
    if (options.key == '招聘') {
      this.getJobDetail()
    }
    if (options.key == '供应商') {
      this.getSupplyDetail()
    }
  },
  onShow: function () {},

  getJobDetail() {
    jobModel.detail(
      {
        id: this.options.id
      },
      (res) => {
        wx.hideLoading()
        console.log(res)
        this.setData({
          detail: res
        })
      }
    )
  },

  getSupplyDetail() {
    supplyModel.detail(
      {
        id: this.options.id
      },
      (res) => {
        wx.hideLoading()
        console.log(res)
        this.setData({
          detail: res
        })
      }
    )
  },

  wxPay() {
    // 微信支付
    const that = this
    wx.login({
      success(res) {
        if (res.code) {
          paymentModel.wxPay(
            {
              code: res.code,
              mapParam: {
                [that.data.options.key]: that.data.detail
              },
              payMoney: that.data.detail.issuePrice,
              payType: 1
            },
            (res) => {
              wx.requestPayment({
                timeStamp: res.timeStamp,
                nonceStr: res.nonceStr,
                package: res.package,
                signType: res.signType,
                paySign: res.paySign,
                success(res) {
                  paymentModel.paySuccess({
                    code: res.code,
                    outTradeNo: res.out_trade_no,
                    mapParam: {
                      [that.data.options.key]: that.data.detail
                    },
                    payMoney: that.data.detail.issuePrice,
                    payType: 1
                  })
                  wx.reLaunch({
                    url: '/packageA/pages/pay-success/pay-success?desc=平台将在24小时内完成审核，请耐心等待！'
                  })
                },
                fail(res) {
                  console.log(res)
                  wx.showToast({
                    title: '支付失败，如有疑问请联系客服~',
                    icon: 'none'
                  })
                }
              })
            }
          )
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }
})
